package com.yanqu.road.server.manger.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.*;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.bussiness.activity.UnionWarActivityBusiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionwar.UnionWarActivityModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 商会商战管理
 */
public class UnionPhaseWarMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(UnionPhaseWarMgr.class.getName());

    //activityId
    private static Map<Integer, UnionWar> unionWarMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                //参赛资格名单
                for (Map.Entry<Integer, List<UnionWarUnionJoin>> entry : unionWar.getUnionWarNameList().entrySet()) {
                    List<UnionWarUnionJoin> nameLists =  entry.getValue();
                    int size = nameLists.size();
                    for(int i = 0; i < size; i++){
                        UnionWarUnionJoin unionName = nameLists.get(i);
                        if(unionName.isInsertOption()){
                            UnionWarActivityBusiness.addUnionWarNameList(unionName);
                        }else if(unionName.isUpdateOption()) {
                            UnionWarActivityBusiness.updateUnionWarNameList(unionName);
                        }
                    }
                }
                //场次商会成员
                for(Map.Entry<String, Map<Integer, Map<Integer, UnionWarUnionMember>>> unionEntry : unionWar.getUnionMemberMap().entrySet()){
                    for(Map.Entry<Integer, Map<Integer, UnionWarUnionMember>> phaseEntry : unionEntry.getValue().entrySet()){
                        for(Map.Entry<Integer, UnionWarUnionMember> sessionEntry : phaseEntry.getValue().entrySet()){
                            UnionWarUnionMember unionMember = sessionEntry.getValue();
                            if(unionMember.isInsertOption()){
                                UnionWarActivityBusiness.addUnionWarUnionMember(unionMember);
                            }else if(unionMember.isUpdateOption()){
                                UnionWarActivityBusiness.updateUnionWarUnionMember(unionMember);
                            }
                        }
                    }
                }

                //备战数据
                for(Map.Entry<String, Map<Integer, Map<Integer, UnionWarDispatchDetail>>> unionEntry : unionWar.getUnionWarDispatchDetailList().entrySet()){
                    for(Map.Entry<Integer, Map<Integer, UnionWarDispatchDetail>> phaseEntry : unionEntry.getValue().entrySet()){
                        for(Map.Entry<Integer, UnionWarDispatchDetail> sessionEntry : phaseEntry.getValue().entrySet()){
                            UnionWarDispatchDetail detail = sessionEntry.getValue();
                            if(detail.isInsertOption()){
                                UnionWarActivityBusiness.addUnionWarDispatchDetail(detail);
                            }else if(detail.isUpdateOption()){
                                UnionWarActivityBusiness.updateUnionWarActivityBusiness(detail);
                            }
                        }
                    }
                }
                for (Map<Integer, Map<Long, UnionWarRankMember>> mapMap : unionWar.getCanGetRankRewardMemberMap().values()) {
                    for (Map<Long, UnionWarRankMember> map : mapMap.values()) {
                        for (UnionWarRankMember data : map.values()) {
                            if (data.isInsertOption()) {
                                UnionWarActivityBusiness.addUnionWarRankMember(data);
                            }
                        }
                    }
                }

                //门客出战
                for(Map.Entry<Long, Map<Integer, UserPatronsSendTimes>> userPatronsEntry : unionWar.getUserPatronsSendMap().entrySet()){
                    for(Map.Entry<Integer, UserPatronsSendTimes> patronsEntry : userPatronsEntry.getValue().entrySet()){
                        UserPatronsSendTimes data = patronsEntry.getValue();
                        if(data.isInsertOption()){
                            UnionWarActivityBusiness.addUserPatronsSendTimes(data);
                        }else if(data.isUpdateOption()){
                            UnionWarActivityBusiness.updateUserPatronsSendTimes(data);
                        }
                    }
                }

                for(Map.Entry<Long, Map<String, UserVehicleSendTimes>> userVehicleEntry : unionWar.getUserVehicleSendMap().entrySet()){
                    for(Map.Entry<String, UserVehicleSendTimes> vehicleEntry : userVehicleEntry.getValue().entrySet()){
                        UserVehicleSendTimes data = vehicleEntry.getValue();
                        if(data.isInsertOption()){
                            UnionWarActivityBusiness.addUserVehicleSendTimes(data);
                        }else if(data.isUpdateOption()){
                            UnionWarActivityBusiness.updateUserVehicleSendTimes(data);
                        }
                    }
                }

                for(Map.Entry<Long, Map<Integer, UserFishSendTimes>> userFishEntry : unionWar.getUserFishSendMap().entrySet()){
                    for(Map.Entry<Integer, UserFishSendTimes>  fishEntry : userFishEntry.getValue().entrySet()){
                        UserFishSendTimes data = fishEntry.getValue();
                        if(data.isInsertOption()){
                            UnionWarActivityBusiness.addUserFishSendTimes(data);
                        }else if(data.isUpdateOption()){
                            UnionWarActivityBusiness.updateUserFishSendTimes(data);
                        }
                    }
                }

                for(Map.Entry<Long, Map<Integer, UserCricketSendTimes>> userCricketEntry : unionWar.getUserCricketSendMap().entrySet()){
                    for(Map.Entry<Integer, UserCricketSendTimes>  cricketEntry : userCricketEntry.getValue().entrySet()){
                        UserCricketSendTimes data = cricketEntry.getValue();
                        if(data.isInsertOption()){
                            UnionWarActivityBusiness.addUserCricketSendTimes(data);
                        }else if(data.isUpdateOption()){
                            UnionWarActivityBusiness.updateUserCricketSendTimes(data);
                        }
                    }
                }
            }
        }
        
        return true;
    }

    public static Map<Integer, UnionWar> getUnionWarMap() {
        return unionWarMap;
    }

    /**
     * 活动重载
     * @param activityInfoMap
     * @param activityMallInfoMap
     * @param activityConditionInfoMap
     */
    public static void reload(Map<Integer, ActivityInfo> activityInfoMap, Map<Integer, Map<Integer, MallInfo>> activityMallInfoMap, Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoMap){
         //加载新活动
         for(ActivityInfo activityInfo : activityInfoMap.values()){
             int activityId = activityInfo.getActivityId();
             if(unionWarMap.containsKey(activityId)){
                 UnionWar unionWar = unionWarMap.get(activityId);
                 //活动信息更新
                 unionWar.setActivityInfo(activityInfo, activityMallInfoMap.get(activityId), activityConditionInfoMap.get(activityId));
             }else{
                 UnionWar unionWar = new UnionWar(activityInfo, activityMallInfoMap.get(activityId), activityConditionInfoMap.get(activityId));
                 unionWar.loadActivityData();
                 unionWarMap.put(activityId, unionWar);
                 unionWar.playerReloadActivity();
             }
         }
         //卸载旧活动
         for(int activity : unionWarMap.keySet()){
             if(!activityInfoMap.containsKey(activity)){
                 UnionWar unionWar = unionWarMap.remove(activity);
                 unionWar.removeActivityData();
             }
         }
    }

    /**
     * 是否可解赛商会
     * @param unionUid
     * @return
     */
    public static boolean canDeleteUnion(String unionUid){
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                if(!unionWar.canDeleteUnion(unionUid)){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 每个场次锁定商会成员
     */
    public static void lockUnionMember() {
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                //unionWar.lockUnionMember();
            }
        }
    }

    /**
     * 定期同步备战数据到跨服
     */
    public static void syncUnionWarPrepareData(){
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                unionWar.syncUnionWarPrepareData();
            }
        }
    }

    /**
     * 确定参战资格名单及推送跨服
     */
    public static void initCrossUnionWarUnion() {
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                 unionWar.initCrossUnionWarUnion();
            }
        }
    }

    public static List<ActivityConditionInfo> getActivityConditionInfoList(int activityId, int conditionType) {
        if (!unionWarMap.containsKey(activityId)) {
            return null;
        }
        return unionWarMap.get(activityId).getActivityConditionInfoList(conditionType);
    }

    public static Map<Integer, List<ActivityConditionInfo>> getActivityConditionInfoMap(int activityId) {
        if (!unionWarMap.containsKey(activityId)) {
            return null;
        }
        return unionWarMap.get(activityId).getActivityConditionInfoMap();
    }

    public static ActivityConditionInfo getActivityConditionInfo(int activityId, int conditionId) {
        Map<Integer, List<ActivityConditionInfo>> dataMap = getActivityConditionInfoMap(activityId);
        if(null != dataMap) {
            for (List<ActivityConditionInfo> conditionInfoList : dataMap.values()) {
                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    if (conditionId == conditionInfo.getConditionId()) {
                        return conditionInfo;
                    }
                }
            }
        }
        return null;
    }

    public static ActivityConditionInfo getActivityConditionInfoByRank(int activityId, int type, int rank) {
        List<ActivityConditionInfo> dataList = getActivityConditionInfoList(activityId, type);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (rank >= conditionInfo.getParamList().get(0).intValue() && rank <= conditionInfo.getParamList().get(1).intValue()) {
                    return conditionInfo;
                }
            }
        }
        return null;
    }

    public static UnionWar getUnionWar(int activityId) {
        if (!unionWarMap.containsKey(activityId)) {
            return null;
        }
        return unionWarMap.get(activityId);
    }

    /**
     * 请求每轮次的战斗结果
     */
    public static void requestBattleResult() {
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                unionWar.requestBattleResult();
            }
        }
    }

    public static void requestCrossSyncDetail() {
        for (int activityId : unionWarMap.keySet()) {
            UnionWar unionWar = unionWarMap.get(activityId);
            if (unionWar != null) {
                unionWar.requestCrossSyncDetail();
            }
        }
    }

    /**
     * 备战界面概览
     * @param activityId
     * @param unionUid
     * @param phase
     * @param beginSession
     * @param endSession
     */
    public static int prepareOverview(GamePlayer player, int activityId, String unionUid, int phase, int beginSession, int endSession) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return unionWar.prepareOverview(player, unionUid, phase, beginSession, endSession);
    }

    /**
     * 阵容未派遣的成员
     * @param player
     * @param activityId
     * @param unionUid
     * @param phase
     * @param session
     * @return
     */
    public static int getNoDispatchMember(GamePlayer player, int activityId, String unionUid, int phase, int session) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return unionWar.getNoDispatchMember(player, unionUid, phase, session);
    }

    /**
     * 备战界面-阵容详情
     * @param player
     * @param activityId
     * @param unionUid
     * @param phase
     * @param session
     * @return
     */
    public static int prepareDetail(GamePlayer player, int activityId, String unionUid, int phase, int session) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return unionWar.prepareDetail(player, unionUid, phase, session);
    }

    /**
     * 锦囊使用
     * @param player
     * @param activityId
     * @param unionUid
     * @param phase
     * @param session
     * @param goodsId
     * @return
     */
    public static int dispatchUserGoods(GamePlayer player, int activityId, String unionUid, int phase, int session, int goodsId) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return unionWar.dispatchUserGoods(player, unionUid, phase, session, goodsId);
    }

    /**
     * 决赛返还被淘汰玩家的锦囊;v2.6
     */
    public static void backUserGoodsInFinals() {
        for(int activityId : unionWarMap.keySet()){
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null){
                unionWar.backUserGoodsInFinals();
            }
        }
    }

    /**
     * 决赛返还被淘汰玩家的锦囊;v2.6
     */
    public static void backUserGoodsInFinalsFromCross(int activityId, CrossUnionWarProto.CrossGetEliminationInfoRespMsg respMsg) {
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar != null){
            unionWar.backUserGoodsInFinalsFromCross(respMsg);
        }
    }

    /**
     * 门客派遣
     * @param player
     * @param activityId
     * @param unionUid
     * @param phase
     * @param session
     * @param patronsIdList
     * @return
     */
    public static int dispatchPatrons(GamePlayer player, int activityId, String unionUid, int phase, int session, List<Integer> patronsIdList) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return unionWar.dispatchPatrons(player, unionUid, phase, session, patronsIdList);
    }

    /**
     * 捐献银两
     * @param player
     * @param activityId
     * @param unionUid
     * @param phase
     * @param session
     * @param silverNum
     * @return
     */
    public static int donateSilver(GamePlayer player, int activityId, String unionUid, int phase, int session, BigInteger silverNum) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return unionWar.donateSilver(player, unionUid, phase, session, silverNum);
    }

    /**
     * 获取商会的积分、排名（后面改为从排行榜获取）
     * @param player
     * @param activityId
     * @param unionUid
     * @param phase
     * @return
     */
    public static int getUnionScoreRank(GamePlayer player, int activityId, String unionUid, int phase) {
        //活动数据完整性
        UnionWar unionWar = unionWarMap.get(activityId);
        if(unionWar == null){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, unionUid);
        return unionWar.getUnionScoreRank(player, unionUid, phase, groupId);
    }

    /**
     * 加入商会
     * @param unionInfo
     * @param userId
     */
    public static void unionMemberJoin(UnionInfo unionInfo, long userId) {
        //玩家在线，重新下发玩家数据
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(player != null){
            for(int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                player.getModule(UnionWarActivityModule.class).syncUserActivityData(activityInfo);

                UnionWar unionWar = unionWarMap.get(activityId);
                if(unionWar != null && unionWar.isJoin(unionInfo.getUnionUid())) {
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionInfo.getUnionUid(), currentTimeInfo);
                        for(UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                detail.setLastUpdateTime(System.currentTimeMillis());
                            }
                        }
//                        detailList = getInPrepareDataByEnemyUnionUid(unionWar, unionInfo.getUnionUid(), currentTimeInfo);
//                        for(UnionWarDispatchDetail detail : detailList) {
//                            synchronized (detail) {
//                                UnionBaseInfo unionBaseInfo = CrossUnionMgr.parseUnionBaseInfo(unionInfo.getUnionUid());
//                                if (unionBaseInfo != null) {
//                                    detail.setEnemyUnionInfo(unionBaseInfo);
//                                }
//                                detail.setLastUpdateTime(System.currentTimeMillis());
//                            }
//                        }
                    }
                }
            }
        }
        unionInfoChange(unionInfo);
    }

    /**
     * 玩家退出商会
     * @param userId
     * @param unionInfo
     */
    public static void quitUnion(long userId, UnionInfo unionInfo) {
        for(int activityId : unionWarMap.keySet()){
            ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
            if(activityInfo == null){
                continue;
            }
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null && unionWar.isJoin(unionInfo.getUnionUid())){
                //需要分组的活动，不撤回备战
                if(UnionActivityGroupHelper.needGroup(unionWar.getActivityInfo())){
                    continue;
                }
                CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                if(currentTimeInfo.getTimeInfo() != null){
                    Property backProperty = new Property();
                    List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionInfo.getUnionUid(), currentTimeInfo);
                    for(UnionWarDispatchDetail detail : detailList) {
                        UnionWarUserPatrons unionWarUserPatrons;
                        synchronized (detail) {
                            for (Map.Entry<Long, UnionWarUserPatrons> patronsEntry : detail.getPatronsMap().entrySet()) {
                                if (patronsEntry.getKey() == userId && patronsEntry.getValue().getGoodsId() != 0) {
                                    backProperty.addProperty(patronsEntry.getValue().getGoodsId(), BigInteger.ONE);
                                }
                            }
                            UnionWarUserSilver userSilver = detail.getSilverMap().get(userId);
                            if (userSilver != null) {
                                userSilver.setQuit(true);
                            } else {
                                detail.getUserBaseInfoMap().remove(userId);
                            }
                            unionWarUserPatrons = detail.getPatronsMap().remove(userId);
                            detail.setLastUpdateTime(System.currentTimeMillis());
                        }
                        //返还门客、珍兽出战次数
                        if(unionWarUserPatrons != null){
                            unionWar.giveBackPatronsVehicleWarTimes(unionWarUserPatrons);
                        }
                    }

                    if (!backProperty.isNothing()) {
                        UserInfo userInfo = UserMgr.getUserInfo(userId);
                        String title = MultipleLanguageMgr.getContent(MailManager.UNION_WAR_ACTIVITY_QUIT_UNION_JIN_NANG_BACK_TITLE, userInfo.getLanguage());
                        String content = MultipleLanguageMgr.getContent(MailManager.UNION_WAR_ACTIVITY_QUIT_UNION_JIN_NANG_BACK_CONTENT, userInfo.getLanguage());
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(backProperty), content, title);
                    }
//                    detailList = getInPrepareDataByEnemyUnionUid(unionWar, unionInfo.getUnionUid(), currentTimeInfo);
//                    for(UnionWarDispatchDetail detail : detailList) {
//                        synchronized (detail) {
//                            UnionBaseInfo unionBaseInfo = UnionHelper.parseUnionBaseInfo(unionInfo, UserMgr.getUserInfo(unionInfo.getMasterUserId()), GameServer.getInstance().getServerId());
//                            detail.setEnemyUnionInfo(unionBaseInfo);
//                            detail.setLastUpdateTime(System.currentTimeMillis());
//                        }
//                    }

                }
            }
            //玩家在线，重新下发玩家数据
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            if(player != null){
                player.getModule(UnionWarActivityModule.class).syncUserActivityData(activityInfo);
            }
        }
        unionInfoChange(unionInfo);
    }

    /**
     * 玩家被踢
     * @param unionInfo
     * @param kickUserId
     */
    public static void kickPlayer(UnionInfo unionInfo, long kickUserId) {
        quitUnion(kickUserId, unionInfo);
    }

    /**
     * 商会职位变更
     * @param unionInfo
     */
    public static void memberPositionChange(UnionInfo unionInfo, long userId, int position) {
        for(int activityId : unionWarMap.keySet()) {
            ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
            if (activityInfo == null) {
                continue;
            }
            UnionWar unionWar = unionWarMap.get(activityId);
            if(unionWar != null && unionWar.isJoin(unionInfo.getUnionUid())){
                CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                if(currentTimeInfo.getTimeInfo() != null){
                    List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionInfo.getUnionUid(), currentTimeInfo);
                    for(UnionWarDispatchDetail detail : detailList) {
                        synchronized (detail) {
                            CrossUnionInfo crossUnionInfo = CrossUnionMgr.getUnionInfo(unionInfo.getUnionUid());
                            if (crossUnionInfo != null) {
                                Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
                                for (CrossUnionMember member : unionMemberMap.values()) {
                                    UnionWarUserPatrons userPatrons = detail.getPatronsMap().get(member.getUserId());
                                    if (userPatrons != null) {
                                        userPatrons.setPosition(member.getPosition());
                                        for (UnionWarUserPatronsDetail patronsDetail : userPatrons.getPatrons().values()) {
                                            patronsDetail.setAbility2(unionPositionAndSkillEffectPatronsData(patronsDetail.getAbility(), member.getPosition(), activityInfo, patronsDetail.getSkillAddition()));
                                        }
                                    }
                                    UnionWarUserSilver userSilver = detail.getSilverMap().get(member.getUserId());
                                    if (userSilver != null) {
                                        userSilver.setPosition(member.getPosition());
                                    }
                                }
                            }

                            UnionWarUserPatrons userPatrons = detail.getPatronsMap().get(userId);
                            if (userPatrons != null) {
                                userPatrons.setPosition(position);
                                for (UnionWarUserPatronsDetail patronsDetail : userPatrons.getPatrons().values()) {
                                    patronsDetail.setAbility2(unionPositionAndSkillEffectPatronsData(patronsDetail.getAbility(), position, activityInfo, patronsDetail.getSkillAddition()));
                                }
                            }
                            UnionWarUserSilver userSilver = detail.getSilverMap().get(userId);
                            if (userSilver != null) {
                                userSilver.setPosition(position);
                            }
                            detail.setLastUpdateTime(System.currentTimeMillis());
                        }
                    }
//                    detailList = getInPrepareDataByEnemyUnionUid(unionWar, unionInfo.getUnionUid(), currentTimeInfo);
//                    for(UnionWarDispatchDetail detail : detailList) {
//                        synchronized (detail) {
//                            UnionBaseInfo unionBaseInfo = UnionHelper.parseUnionBaseInfo(unionInfo, UserMgr.getUserInfo(unionInfo.getMasterUserId()), GameServer.getInstance().getServerId());
//                            detail.setEnemyUnionInfo(unionBaseInfo);
//                            detail.setLastUpdateTime(System.currentTimeMillis());
//                        }
//                    }
                }
            }
        }
        if(position == eUnionPosition.Master.getValue()){
            unionInfoChange(unionInfo);
        }
    }

    /**
     * 门客数据变更
     * @param patronsList
     * @param userPatronsMap
     */
    public static void patronsAbilityChange(UserInfo userInfo, List<UserPatrons> patronsList, Map<Integer, UserPatrons> userPatronsMap) {
        try{
            //if(StringUtils.isNullOrEmpty(userInfo.getUnionUid())){
            //    return;
            //}
            for(int activityId : unionWarMap.keySet()) {
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, userInfo.getUserId());
                if(StringUtils.isNullOrEmpty(unionUid)){
                    continue;
                }
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                if(unionWar == null || !unionWar.isJoin(unionUid)){
                    continue;
                }
                CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                if(currentTimeInfo.getTimeInfo() == null){
                    continue;
                }
                List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                for(UnionWarDispatchDetail detail : detailList){
                    synchronized (detail) {
                        //刷新门客数据
                        boolean isChange = false;
                        UnionWarUserPatrons warUserPatrons = detail.getPatronsMap().get(userInfo.getUserId());
                        if (warUserPatrons != null) {
                            for (UnionWarUserPatronsDetail patronsDetail : warUserPatrons.getPatrons().values()) {
                                for (UserPatrons userPatrons : patronsList) {
                                    if (userPatrons.getPatronsId() == patronsDetail.getPatronsId()) {
                                        long ability2 = unionPositionAndSkillEffectPatronsData(userPatrons.getAbility().longValue(), warUserPatrons.getPosition(), activityInfo, patronsDetail.getSkillAddition());
                                        if (ability2 != patronsDetail.getAbility2()) {
                                            isChange = true;
                                        }
                                        patronsDetail.setAbility(userPatrons.getAbility().longValue());
                                        patronsDetail.setSkillAddition(UnionWar.getAbilityTalentAddition(userPatronsMap));
                                        patronsDetail.setAbility2(ability2);
                                        patronsDetail.setPatronsLv(userPatrons.getLevel());
                                        patronsDetail.setPatronsStageLv(userPatrons.getStageLv());
                                        patronsDetail.setSkinId(userPatrons.getSkinId());
                                        patronsDetail.setPromotionId(userPatrons.getShowPromotionId());
                                        patronsDetail.setLianShengAddition(UnionWar.getLianShengTalentAddition(userPatrons));
                                    }
                                }
                            }
                        }
                        if (isChange) {
                            //刷新商会数据
                            detail.setLastUpdateTime(System.currentTimeMillis());
                        }
                    }
                }
//                detailList = getInPrepareDataByEnemyUnionUid(unionWar, userInfo.getUnionUid(), currentTimeInfo);
//                for(UnionWarDispatchDetail detail : detailList){
//                    synchronized (detail) {
//                        UnionBaseInfo unionBaseInfo = UnionHelper.parseUnionBaseInfo(unionInfo, UserMgr.getUserInfo(unionInfo.getMasterUserId()), GameServer.getInstance().getServerId());
//                        detail.setEnemyUnionInfo(unionBaseInfo);
//                        detail.setLastUpdateTime(System.currentTimeMillis());
//                    }
//                }
            }
        }catch(Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 获取商会备战中的阵容数据
     * @param unionWar
     * @param unionUid
     * @return
     */
    public static List<UnionWarDispatchDetail> getInPrepareDataByUnionUid(UnionWar unionWar, String unionUid, CurrentTimeInfo currentTimeInfo){
        List<UnionWarDispatchDetail> result = new ArrayList<>();
        int phase = currentTimeInfo.getTimeInfo().getType();
        int session = currentTimeInfo.getTimeInfo().getSession();
        Map<Integer, UnionWarDispatchDetail> detailMap = unionWar.getUnionWarDispatchDetailList(unionUid, phase);
        if(detailMap == null){
            return result;
        }
        for(Map.Entry<Integer, UnionWarDispatchDetail> entry : detailMap.entrySet()) {
            UnionWarDispatchDetail detail = entry.getValue();
            //场次大于当前场次或者当前场次是备战期
            if ((detail.getSession() > session || (detail.getSession() == session && currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.PrepareTime))) {
                result.add(detail);
            }
        }
        return result;
    }

    /**
     * 获取对手商会备战中的阵容数据
     * @param unionWar
     * @param unionUid
     * @param currentTimeInfo
     * @return
     */
    public static List<UnionWarDispatchDetail> getInPrepareDataByEnemyUnionUid(UnionWar unionWar, String unionUid, CurrentTimeInfo currentTimeInfo){
        List<UnionWarDispatchDetail> result = new ArrayList<>();
        int phase = currentTimeInfo.getTimeInfo().getType();
        int session = currentTimeInfo.getTimeInfo().getSession();
        //备战数据
        for(Map.Entry<String, Map<Integer, Map<Integer, UnionWarDispatchDetail>>> unionEntry : unionWar.getUnionWarDispatchDetailList().entrySet()){
            for(Map.Entry<Integer, Map<Integer, UnionWarDispatchDetail>> phaseEntry : unionEntry.getValue().entrySet()){
                for(Map.Entry<Integer, UnionWarDispatchDetail> sessionEntry : phaseEntry.getValue().entrySet()){
                    UnionWarDispatchDetail detail = sessionEntry.getValue();
                    if(detail.getPhase() < phase){
                        continue;
                    }
                    if(detail.getSession() < session || (detail.getSession() == session && currentTimeInfo.getPeriodType() != eTimeInfoPeriodType.PrepareTime)){
                        continue;
                    }
                    if(unionUid.equals(detail.getEnemyUnionUid())){
                        result.add(detail);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 商会赚速\信息变更\成员变更\会长变更
     * @param unionInfo
     */
    public static void unionInfoChange(UnionInfo unionInfo){
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                if (unionWar != null && unionWar.isJoin(unionInfo.getUnionUid())) {
                    UnionWarUnionJoin unionJoin = unionWar.getUnionWarNameList(unionInfo.getUnionUid());
                    if (unionJoin != null) {
                        UnionBaseInfo unionBaseInfo = CrossUnionMgr.parseUnionBaseInfo(unionInfo.getUnionUid());
                        if (unionBaseInfo != null) {
                            unionJoin.setUnionBaseInfo(unionBaseInfo);
                        }
                        unionJoin.setNeedSync(true);
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 职位与门客加成
     */
    public static long unionPositionAndSkillEffectPatronsData(long ability, int position, ActivityInfo activityInfo, int skillAddition){
        List<Integer> paramList = StringUtils.stringToIntegerList(activityInfo.getParam3(), "\\|");
        if(ability <= 0){
            return 0l;
        }
        BigDecimal blood = new BigDecimal(ability);
        int addition;
        if(position == eUnionPosition.Master.getValue()){
            addition = paramList.get(0);
        }else if(position == eUnionPosition.DeputyMaster.getValue()){
            addition = paramList.get(1);
        }else if(position == eUnionPosition.Elite.getValue()){
            addition = paramList.get(2);
        }else {
            addition = paramList.get(3);
        }
        addition += skillAddition;
        blood = blood.multiply(BigDecimal.valueOf(1000 + addition)).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP);
        return blood.longValue();
    }

    /**
     * 卸载门客的珍兽（返还珍兽出战次数、清除出战记录）
     * @param player
     * @param patronsId
     * @param vehicleUid
     */
    public static void dismountVehicle(GamePlayer player, int patronsId, String vehicleUid) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                        for (UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                if (unionWarUserPatrons != null) {
                                    //珍兽有出战
                                    if (unionWarUserPatrons.getVehicleUidList().containsKey(patronsId)) {
                                        unionWarUserPatrons.getVehicleUidList().remove(patronsId);
                                        detail.setLastUpdateTime(System.currentTimeMillis());
                                        UserVehicleSendTimes sendTimes = unionWar.getUserVehicleSend(player.getUserId(), vehicleUid);
                                        if (sendTimes != null && sendTimes.getTimes() > 0) {
                                            sendTimes.setTimes(sendTimes.getTimes() - 1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 卸载门客的鱼类（返还鱼类出战次数、清除出战记录）
     * @param player
     * @param patronsId
     * @param fishId
     */
    public static void dismountFish(GamePlayer player, int patronsId, int fishId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                        for (UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                if (unionWarUserPatrons != null) {
                                    //鱼类有出战
                                    if (unionWarUserPatrons.getFishIdList().containsKey(patronsId)) {
                                        unionWarUserPatrons.getFishIdList().remove(patronsId);
                                        detail.setLastUpdateTime(System.currentTimeMillis());
                                        UserFishSendTimes sendTimes = unionWar.getUserFishSend(player.getUserId(), fishId);
                                        if (sendTimes != null && sendTimes.getTimes() > 0) {
                                            sendTimes.setTimes(sendTimes.getTimes() - 1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 卸载门客的蛐蛐类（返还类出战次数、清除出战记录）
     * @param player
     * @param patronsId
     * @param cricketId
     */
    public static void dismountCricket(GamePlayer player, int patronsId, int cricketId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                        for (UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                if (unionWarUserPatrons != null) {
                                    //鱼类有出战
                                    if (unionWarUserPatrons.getCricketIdList().containsKey(patronsId)) {
                                        unionWarUserPatrons.getCricketIdList().remove(patronsId);
                                        detail.setLastUpdateTime(System.currentTimeMillis());
                                        UserCricketSendTimes sendTimes = unionWar.getUserCricketSend(player.getUserId(), cricketId);
                                        if (sendTimes != null && sendTimes.getTimes() > 0) {
                                            sendTimes.setTimes(sendTimes.getTimes() - 1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 装备门客的珍兽(增加珍兽出战次数、增加出战记录)
     * @param player
     * @param vehicleUid
     * @param patronsId
     */
    public static void equipVehicle(GamePlayer player, String vehicleUid, int patronsId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                        for (UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                if (unionWarUserPatrons != null) {
                                    //门客有出战
                                    if (unionWarUserPatrons.getPatrons().containsKey(patronsId)) {
                                        unionWarUserPatrons.getVehicleUidList().put(patronsId, vehicleUid);
                                        detail.setLastUpdateTime(System.currentTimeMillis());

                                        UserVehicleSendTimes sendTimes = unionWar.getAndCheckUserVehicleSendTimes(player.getUserId(), vehicleUid);
                                        sendTimes.setTimes(sendTimes.getTimes() + 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 装备门客的鱼类(增加鱼类出战次数、增加出战记录)
     * @param player
     * @param fishId
     * @param patronsId
     */
    public static void equipFish(GamePlayer player, int fishId, int patronsId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                        for (UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                if (unionWarUserPatrons != null) {
                                    //门客有出战
                                    if (unionWarUserPatrons.getPatrons().containsKey(patronsId)) {
                                        unionWarUserPatrons.getFishIdList().put(patronsId, fishId);
                                        detail.setLastUpdateTime(System.currentTimeMillis());

                                        UserFishSendTimes sendTimes = unionWar.getAndCheckUserFishSendTimes(player.getUserId(), fishId);
                                        sendTimes.setTimes(sendTimes.getTimes() + 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    public static void equipCricket(GamePlayer player, int cricketId, int patronsId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                        for (UnionWarDispatchDetail detail : detailList) {
                            synchronized (detail) {
                                UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                if (unionWarUserPatrons != null) {
                                    //门客有出战
                                    if (unionWarUserPatrons.getPatrons().containsKey(patronsId)) {
                                        unionWarUserPatrons.getCricketIdList().put(patronsId, cricketId);
                                        detail.setLastUpdateTime(System.currentTimeMillis());

                                        UserCricketSendTimes sendTimes = unionWar.getAndCheckUserCricketSendTimes(player.getUserId(), cricketId);
                                        sendTimes.setTimes(sendTimes.getTimes() + 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 珍兽是否可装备；门客未出战时可装备（不在备战中或者备战的已开战）
     * 已出战的珍兽，不可装备到备战中的门客
     * @param player
     * @param vehicleUid
     * @return
     */
    public static boolean canMountVehicle(GamePlayer player, String vehicleUid, int patronsId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        UserVehicleSendTimes userVehicleSendTimes = unionWar.getUserVehicleSend(player.getUserId(), vehicleUid);
                        //已出战的珍兽，不可装备到备战中的门客！！！
                        if (userVehicleSendTimes != null && userVehicleSendTimes.getTimes() > 0) {
                            List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                            for (UnionWarDispatchDetail detail : detailList) {
                                if (detail.getPatronsMap().containsKey(player.getUserId())) {
                                    UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                    if (unionWarUserPatrons.getPatrons().containsKey(patronsId)) {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            return false;
        }
        return true;
    }

    /**
     * 鱼类是否可装备；门客未出战时可装备（不在备战中或者备战的已开战）
     * 已出战的鱼类，不可装备到备战中的门客
     * @param player
     * @param fishId
     * @return
     */
    public static boolean canMountFish(GamePlayer player, int fishId, int patronsId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        UserFishSendTimes userFishSendTimes = unionWar.getUserFishSend(player.getUserId(), fishId);
                        //已出战的鱼类，不可装备到备战中的门客！！！
                        if (userFishSendTimes != null && userFishSendTimes.getTimes() > 0) {
                            List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                            for (UnionWarDispatchDetail detail : detailList) {
                                if (detail.getPatronsMap().containsKey(player.getUserId())) {
                                    UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                    if (unionWarUserPatrons.getPatrons().containsKey(patronsId)) {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            return false;
        }
        return true;
    }

    public static boolean canMountCricket(GamePlayer player, int cricketId, int patronsId) {
        try {
            for (int activityId : unionWarMap.keySet()) {
                ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
                if (activityInfo == null) {
                    continue;
                }
                UnionWar unionWar = unionWarMap.get(activityId);
                String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
                if (unionWar != null && !StringUtils.isNullOrEmpty(unionUid)) {
                    if (!unionWar.isJoin(unionUid)) {
                        continue;
                    }
                    CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
                    if (currentTimeInfo.getTimeInfo() != null) {
                        UserCricketSendTimes userCricketSendTimes = unionWar.getUserCricketSend(player.getUserId(), cricketId);
                        //已出战的昆虫类，不可装备到备战中的门客！！！
                        if (userCricketSendTimes != null && userCricketSendTimes.getTimes() > 0) {
                            List<UnionWarDispatchDetail> detailList = getInPrepareDataByUnionUid(unionWar, unionUid, currentTimeInfo);
                            for (UnionWarDispatchDetail detail : detailList) {
                                if (detail.getPatronsMap().containsKey(player.getUserId())) {
                                    UnionWarUserPatrons unionWarUserPatrons = detail.getPatronsMap().get(player.getUserId());
                                    if (unionWarUserPatrons.getPatrons().containsKey(patronsId)) {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            return false;
        }
        return true;
    }
}
